package com.kandinfo.base.common.swagger;

import static com.google.common.collect.Lists.newArrayList;
import static com.mangofactory.swagger.models.property.ApiModelProperties.allowableValueFromString;

import java.util.Collections;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.configuration.SwaggerModelsConfiguration;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.models.dto.Parameter;
import com.mangofactory.swagger.models.dto.builder.ParameterBuilder;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;
import com.mangofactory.swagger.readers.operation.RequestMappingReader;

@Configuration
@ComponentScan(basePackages = { "com.mangofactory.swagger.controllers" })
@Import(SwaggerModelsConfiguration.class)
public class MySpringSwaggerConfig extends SpringSwaggerConfig {

    @SuppressWarnings("unchecked")
	@Bean
    public SwaggerSpringMvcPlugin customImplementation() {

        RequestMappingReader requestMappingReader = requestMappingContext -> {
            List<Parameter> parameters = (List<Parameter>) requestMappingContext.get("parameters");
            if (parameters == null) {
                parameters = newArrayList();
            }

            parameters.add(new ParameterBuilder().name("version").description("版本号").defaultValue("v1").required(true)
                    .allowMultiple(false).dataType("").allowableValues(allowableValueFromString("v1"))
                    .parameterType("path").parameterAccess("").build());
            requestMappingContext.put("parameters", parameters);
        };

        return new SwaggerSpringMvcPlugin(this).apiInfo(apiInfo()).includePatterns(".*").apiVersion("0.0.1")
                .swaggerGroup("user").customAnnotationReaders(Collections.singletonList(requestMappingReader));
    }

    private ApiInfo apiInfo() {
        return new ApiInfo("API", "API文档", "index.html", "", "My License", "");
    }

}
